/**
* ...
* @author Default
* @version 0.1
*/

package bdz.dms.vo{
	
	public class SectionVO extends CoreDataVO {
		private var proplist:Array = ["property_id", "property_name", "property_key", "property_value", "section_id", "property_type"];
		private var propdelete;
		function SectionVO(xm:XML = null){	
			super(xm);
		}
		override protected function initialize():void {
			_properties = new Array();
			if(written){
				var counter:Number = 0;
				for each(var o:* in _localxml.properties.property){
					_properties[counter] = {};
					for each(var s:String in proplist){
						_properties[counter][s] = o[s];
					}
					counter++;
				}
				common_id = _localxml.section_id;
			}
			
		}
		public function get section_id():Number{
			return _localxml.section_id;
		}
		public function get section_name():String{
			return _localxml.section_name;
		}
		public function set section_name(str:String):void{
			_localxml.section_name = str;
		}
		public function get order():Number{
			return _localxml.order;
		}
		public function set order(n:Number):void{
			_localxml.order = n;
		}
		public function get parent_id():Number{
			return _localxml.parent_id;
		}
		public function set parent_id(n:Number):void{
			_localxml.parent_id = n;
			common_id = n;
		}
		public function removeProperty(pid:Number):void{
			if(propdelete == null){
				propdelete = new Array();
				_localxml.deleteprops = "";
			}
			for (var i:Number=0;i<_properties.length;i++){
				if(_properties[i].property_id == pid){
					deletePropIndex(i);
					break;
				}
			}
		}
		private function deletePropIndex(indx:Number):void{
			var xnode:XML = <todelete/>;
			xnode.property_id = _properties[indx].property_id;
			propdelete.push(xnode);
			_properties.splice(indx,1);
		}
		
		public function addProperty(key:String,name:String,value:*,type:String):void{
			var o:Object = {};
			o.property_key = key;
			o.property_name = name;
			o.property_value = value;
			o.property_type = type;
			o.section_id = _localxml.section_id;
			var matched:Boolean = false;
			for each(var obj:Object in _properties){
				if(obj.property_key == o.property_key){
					matched = true;
					obj.property_value = value;
					break;
				}
			}
			if(!matched){
				_properties.push(o);
			}
		
		}
		override public function toXML():XML{
			var xvalue:XML = <dms/>;
			xvalue.section_id = _localxml.section_id;
			xvalue.section_name = _localxml.section_name;
			xvalue.order = _localxml.order;
			xvalue.parent_id = _localxml.parent_id;
			if(_properties.length){
				xvalue.properties = "";
			}
			for (var i:Number = 0; i <_properties.length; i++) {
				var xnode:XML = <property/>
				for each(var s:String in proplist){
					if(s == "section_id"){
						xnode[s] == _localxml.section_id;
					}else{
						if(_properties[i][s]){
							xnode[s] = _properties[i][s];
						}else{
							if(s != "property_id"){
								xnode[s] = "";
							}
						}
					}
				}
				xvalue.properties.property[i] = xnode;
			}
			if(propdelete){
				xvalue.deleteprops = ""
				for(i = 0;i<propdelete.length;i++){
					xvalue.deleteprops.todelete[i] = propdelete[i];
				}
			}
			return xvalue;
		}
		
	}
	
}